﻿<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<ScrumTime.Models.Task>" %>

<script runat="server" type="text/C#">

    System.Data.Objects.DataClasses.EntityCollection<ScrumTime.Models.User> GetUsers()
    {                
        
        int ProductId = ScrumTime.Helpers.SessionHelper.GetCurrentProductId(this.Session);
        ScrumTime.Services.ProductService productService = new ScrumTime.Services.ProductService();
        ScrumTime.Models.Product product = productService.GetProductById(ProductId);
        return product.Users;
    }

    decimal GetHoursWorked() {
        decimal resultado = 0;

        ScrumTime.Models.ScrumTimeEntities scrumTimeEntities = new ScrumTime.Models.ScrumTimeEntities();
        var results = from t in scrumTimeEntities.ScrumDetails
                      where t.TaskId == Model.TaskId
                      orderby t.Scrum.DateOfScrum descending
                      select t;

        if (results.Count() > 0) {
            resultado = results.First<ScrumTime.Models.ScrumDetail>().HoursCompleted;
        }
        
        return resultado;
    }
</script>

<script>


        function getLiOfOl( nameOfOl) {
            var testlist = [];

            $('#ol-task-uaffectedtask li').each(function () {
                testlist.push($(this).attr('affectedTaskId'));
            });
            
            return testlist.join("-");
        }


        function showStory_save_task() {
            var storyId = $('#showStory_Task_StoryId').val();
            var taskId = $('#showStory_Task_TaskId').val();

            var description = $('#showStoryTask-description').val();
            var hours = $('#showStoryTask-hours').val();
            var usabilityhours = $('#showStoryTask-usability_hours').val();
            

            var assignedTo = $('#showStoryTask-AssignedTo').val();

            var v_usabilityAffected_task = getLiOfOl();
            var url = addURLRamdon('/Task/Save');
                $.post(url,
                {
                    storyId: storyId, taskId: taskId,
                    description: description, hours: hours, assignedTo: assignedTo,
                    usabilityaffectedtasks: v_usabilityAffected_task, usabilityhours: usabilityhours
                },
                function (data) {
                    if (verificarError(data)) return;
                    LoadTasks();
                }
            );
        }

        function ShowStoryTaskSave() {
            showStory_save_task(); 
            return true;
        }

        function ShowStoryTaskCancel(){
            LoadTasks();
            return true;
        }

        function LoadTasks() {
            var storyid = $("#show-story-basicdata-idStory").val();
            var url = addURLRamdon('/Story/ShowStory_Tasks');
            $.post(url,
                {
                    id: storyid
                },
                function (data) {
                    if (verificarError(data)) return;
                    $("#div_main_show_story_taskList").html(data);
                }
            );

        }

</script>
<input type="hidden" id="showStory_Task_TaskId" />
<input type="hidden" id="showStory_Task_StoryId" />
<table style="width:100%;">
    <tr>
        <td>
            <table>
                <tr>
                    <td>
                        id:
                    </td>
                    <td colspan="3">
                        <input type="text" id="showStoryTask-id"  disabled="disabled" />
                    </td>
                </tr>
                <tr>
                    <td>
                        description:
                    </td>
                    <td colspan="3">
                        <textarea id="showStoryTask-description" rows="3" cols="50"><%:Model.Description %></textarea>
                    </td>
                </tr>
                <tr>
                    <td>
                        estimated hours: 
                    </td>
                    <td>
                        <input  id="showStoryTask-hours"type="text"  />
                    </td>
                    <td>
                        assigned to:
                    </td>
                    <td>
                        <select id="showStoryTask-AssignedTo" >
                            <option value="" >-Unassigned-</option>
                            <%                                
                                foreach( ScrumTime.Models.User user in GetUsers()){
                                    if (user.UserId.ToString() == Model.AssignedToUserId.ToString())
                                    {
                                %>                                    
                                    <option value="<%:user.UserId.ToString()%>" selected ><%:user.UserName%></option>
                                <%  }
                                    else { 
                                %>                                    
                                    <option value="<%:user.UserId.ToString()%>" ><%:user.UserName%></option>
                                <%     
                                    }
                                }
                            %>
                        </select>
                    </td>
                </tr>
                <tr>
                    <td>
                        hours worked: 
                    </td>
                    <td>
                        <input  id="showStoryTask_hours_worked"type="text"  disabled />
                    </td>
                    <td>                        
                        usability Hours: 
                    </td>
                    <td>
                        <input id="showStoryTask-usability_hours"></input>
                    </td>
                </tr>
            </table>
        </td>
    </tr>
    <tr>
        <td>
            <div id="div-info-task-uaffectedtask" style="border-width:1px;">
                <span style="font-weight:bold">Usability Affected Tasks</span> 
                    <ol id="ol-task-uaffectedtask">
			            <%
                            foreach (ScrumTime.Models.UsabilityAffectedTask uat in Model.UsabilityAffectedTasks) { 
                            %>
                                <li id="li_at_<%:uat.UsabilityAffectedTaskId%>"  affectedTaskId="<%:uat.UsabilityAffectedTaskId%>" > <%:uat.Description%> <a href="#" onclick="remove_li_af(<%:uat.UsabilityAffectedTaskId%>)">&lt;remove&gt;</a> </li>
                            <%
                            }
                        %>
		            </ol>
            </div>
        </td>
    </tr>
    <tr>
        <td>
            <button id="show-story-task-button-edit" onclick="ShowStoryTaskSave();">Save</button>
            <button id="show-story-task-button-cancel"  onclick="ShowStoryTaskCancel();">Cancel</button>
        </td>    
    </tr>

</table>

<script>
    
         $("#show-story-task-button-edit").button();
         $("#show-story-task-button-cancel").button();
         // set values
         $("#showStory_Task_TaskId").val('<%:Model.TaskId %>');
         $("#showStory_Task_StoryId").val('<%:Model.StoryId %>');
         $("#showStoryTask-id").val('<%:Model.TaskId %>');
         
         $("#showStoryTask-hours").val('<%: Model.Hours%>');
         $("#showStoryTask_hours_worked").val('<%: GetHoursWorked()%>');
         $("#showStoryTask-usability_hours").val('<%: Model.UsabilityHours%>');
         

</script>